#ifndef WAYPOINTPARSER_H
#define WAYPOINTPARSER_H

#include <QLinkedList>
#include <QXmlDefaultHandler>
#include "Waypoint.h"

class WaypointParser: public QXmlDefaultHandler {

private:
    bool inDoc;

public:
    QLinkedList<Waypoint> list;
    bool startDocument(){
        inDoc = false;
        return true;
    }

    bool endElement( const QString&, const QString&, const QString &name ){
        if( name == "pos" ){
            inDoc = false;
        }

        return true;
    }

    bool startElement( const QString&, const QString&, const QString &name, const QXmlAttributes &attrs ){
        if( inDoc && name == "waypoint" ){
            QString desc;
            qreal lng, lat;

            for( int i=0; i<attrs.count(); i++ ){
                if( attrs.localName( i ) == "longitude" )
                  lng = (attrs.value( i )).toFloat();
                else if( attrs.localName( i ) == "latitude" )
                  lat = (attrs.value( i )).toFloat();
                else if( attrs.localName( i ) == "description" )
                  desc = attrs.value( i );

                list.append(Waypoint(desc, lat, lng));
            }
        }
        else if( name == "pos" )
            inDoc = true;

        return true;
    }
};

#endif // WAYPOINTPARSER_H
